3 * Cache for article titles (prefixed DB keys) and ids linked from one source
11 # These are used in incrementalSetup()
12 define ('LINKCACHE_GOOD', 0);
13 define ('LINKCACHE_BAD', 1);
14 define ('LINKCACHE_IMAGE', 2);
15 define ('LINKCACHE_PAGE', 3);
22 // Increment $mClassVer whenever old serialized versions of this class
23 // becomes incompatible with the new version.
24 /* private */ var $mClassVer = 3;
26 /* private */ var $mPageLinks;
27 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
28 /* private */ var $mImageLinks, $mCategoryLinks;
29 /* private */ var $mPreFilled, $mOldGoodLinks, $mOldBadLinks;
30 /* private */ var $mForUpdate;
32 /* private */ function getKey( $title ) {
34 return $wgDBname.':lc:title:'.$title;
37 function LinkCache() {
38 $this->mActive
= true;
39 $this->mPreFilled
= false;
40 $this->mForUpdate
= false;
41 $this->mPageLinks
= array();
42 $this->mGoodLinks
= array();
43 $this->mBadLinks
= array();
44 $this->mImageLinks
= array();
45 $this->mCategoryLinks
= array();
46 $this->mOldGoodLinks
= array();
47 $this->mOldBadLinks
= array();
48 $this->mOldPageLinks
= array();
52 * General accessor to get/set whether SELECT FOR UPDATE should be used
54 function forUpdate( $update = NULL ) {
55 return wfSetVar( $this->mForUpdate
, $update );
58 function getGoodLinkID( $title ) {
59 if ( array_key_exists( $title, $this->mGoodLinks
) ) {
60 return $this->mGoodLinks
[$title];
66 function isBadLink( $title ) {
67 return array_key_exists( $title, $this->mBadLinks
);
70 function addGoodLinkObj( $id, $title ) {
71 if ( $this->mActive
) {
72 $dbkey = $title->getPrefixedDbKey();
73 $this->mGoodLinks
[$dbkey] = $id;
74 $this->mPageLinks
[$dbkey] = $title;
78 function addBadLinkObj( $title ) {
79 $dbkey = $title->getPrefixedDbKey();
80 if ( $this->mActive
&& ( ! $this->isBadLink( $dbkey ) ) ) {
81 $this->mBadLinks
[$dbkey] = 1;
82 $this->mPageLinks
[$dbkey] = $title;
86 function addImageLink( $title ) {
87 if ( $this->mActive
) { $this->mImageLinks
[$title] = 1; }
90 function addImageLinkObj( $nt ) {
91 if ( $this->mActive
) { $this->mImageLinks
[$nt->getDBkey()] = 1; }
94 function addCategoryLink( $title, $sortkey ) {
95 if ( $this->mActive
) { $this->mCategoryLinks
[$title] = $sortkey; }
98 function addCategoryLinkObj( &$nt, $sortkey ) {
99 $this->addCategoryLink( $nt->getDBkey(), $sortkey );
102 function clearBadLink( $title ) {
103 unset( $this->mBadLinks
[$title] );
104 $this->clearLink( $title );
107 function clearLink( $title ) {
108 global $wgMemc, $wgLinkCacheMemcached;
109 if( $wgLinkCacheMemcached )
110 $wgMemc->delete( $this->getKey( $title ) );
114 function suspend() { $this->mActive
= false; }
116 function resume() { $this->mActive
= true; }
118 function getPageLinks() { return $this->mPageLinks
; }
119 function getGoodLinks() { return $this->mGoodLinks
; }
120 function getBadLinks() { return array_keys( $this->mBadLinks
); }
121 function getImageLinks() { return $this->mImageLinks
; }
122 function getCategoryLinks() { return $this->mCategoryLinks
; }
125 * Add a title to the link cache, return the page_id or zero if non-existent
126 * @param string $title Title to add
129 function addLink( $title ) {
130 $nt = Title
::newFromDBkey( $title );
132 return $this->addLinkObj( $nt );
139 * Add a title to the link cache, return the page_id or zero if non-existent
140 * @param Title $nt Title to add
143 function addLinkObj( &$nt ) {
144 global $wgMemc, $wgLinkCacheMemcached, $wgAntiLockFlags;
145 $title = $nt->getPrefixedDBkey();
146 if ( $this->isBadLink( $title ) ) { return 0; }
147 $id = $this->getGoodLinkID( $title );
148 if ( 0 != $id ) { return $id; }
150 $fname = 'LinkCache::addLinkObj';
151 global $wgProfiling, $wgProfiler;
152 if ( $wgProfiling && isset( $wgProfiler ) ) {
153 $fname .= ' (' . $wgProfiler->getCurrentSection() . ')';
156 wfProfileIn( $fname );
158 $ns = $nt->getNamespace();
159 $t = $nt->getDBkey();
161 if ( '' == $title ) {
162 wfProfileOut( $fname );
167 if( $wgLinkCacheMemcached )
168 $id = $wgMemc->get( $key = $this->getKey( $title ) );
169 if( ! is_integer( $id ) ) {
170 if ( $this->mForUpdate
) {
171 $db =& wfGetDB( DB_MASTER
);
172 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
173 $options = array( 'FOR UPDATE' );
178 $db =& wfGetDB( DB_SLAVE
);
182 $id = $db->selectField( 'page', 'page_id',
183 array( 'page_namespace' => $ns, 'page_title' => $t ),
188 if( $wgLinkCacheMemcached )
189 $wgMemc->add( $key, $id, 3600*24 );
193 $this->addBadLinkObj( $nt );
195 $this->addGoodLinkObj( $id, $nt );
197 wfProfileOut( $fname );
202 * Bulk-check the pagelinks and page arrays for existence info.
203 * @param Title $fromtitle
206 function preFill( &$fromtitle ) {
207 global $wgAntiLockFlags;
208 $fname = 'LinkCache::preFill';
209 wfProfileIn( $fname );
212 $id = $fromtitle->getArticleID();
216 wfDebug( "$fname - got id 0 for title '" . $fromtitle->getPrefixedDBkey() . "'\n" );
217 wfProfileOut( $fname );
221 if ( $this->mForUpdate
) {
222 $db =& wfGetDB( DB_MASTER
);
223 if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) ) {
224 $options = 'FOR UPDATE';
229 $db =& wfGetDB( DB_SLAVE
);
233 $page = $db->tableName( 'page' );
234 $pagelinks = $db->tableName( 'pagelinks' );
236 $sql = "SELECT page_id,pl_namespace,pl_title
239 ON pl_namespace=page_namespace AND pl_title=page_title
240 WHERE pl_from=$id $options";
241 $res = $db->query( $sql, $fname );
242 while( $s = $db->fetchObject( $res ) ) {
243 $title = Title
::makeTitle( $s->pl_namespace
, $s->pl_title
);
245 $this->addGoodLinkObj( $s->page_id
, $title );
247 $this->addBadLinkObj( $title );
250 $this->mPreFilled
= true;
252 wfProfileOut( $fname );
259 $this->mPageLinks
= array();
260 $this->mGoodLinks
= array();
261 $this->mBadLinks
= array();
262 $this->mImageLinks
= array();
263 $this->mCategoryLinks
= array();
264 $this->mOldGoodLinks
= array();
265 $this->mOldBadLinks
= array();
266 $this->mOldPageLinks
= array();
270 * Swaps old and current link registers
273 function swapRegisters() {
274 swap( $this->mGoodLinks
, $this->mOldGoodLinks
);
275 swap( $this->mBadLinks
, $this->mOldBadLinks
);
276 swap( $this->mImageLinks
, $this->mOldImageLinks
);
277 swap( $this->mPageLinks
, $this->mOldPageLinks
);
282 * Class representing a list of titles
283 * The execute() method checks them all for existence and adds them to a LinkCache object
290 * 2-d array, first index namespace, second index dbkey, value arbitrary
294 function LinkBatch( $arr = array() ) {
295 foreach( $arr as $item ) {
296 $this->addObj( $item );
300 function addObj( $title ) {
301 if ( is_object( $title ) ) {
302 $this->add( $title->getNamespace(), $title->getDBkey() );
304 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
308 function add( $ns, $dbkey ) {
312 if ( !array_key_exists( $ns, $this->data
) ) {
313 $this->data
[$ns] = array();
316 $this->data
[$ns][$dbkey] = 1;
320 * Set the link list to a given 2-d array
321 * First key is the namespace, second is the DB key, value arbitrary
323 function setArray( $array ) {
324 $this->data
= $array;
328 * Do the query and add the results to a LinkCache object
329 * Return an array mapping PDBK to ID
331 function execute( &$cache ) {
332 $fname = 'LinkBatch::execute';
333 wfProfileIn( $fname );
335 $res = $this->doQuery();
337 wfProfileOut( $fname );
341 // For each returned entry, add it to the list of good links, and remove it from $remaining
344 $remaining = $this->data
;
345 while ( $row = $res->fetchObject() ) {
346 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
347 $cache->addGoodLinkObj( $row->page_id
, $title );
348 $ids[$title->getPrefixedDBkey()] = $row->page_id
;
349 unset( $remaining[$row->page_namespace
][$row->page_title
] );
353 // The remaining links in $data are bad links, register them as such
354 foreach ( $remaining as $ns => $dbkeys ) {
355 foreach ( $dbkeys as $dbkey => $nothing ) {
356 $title = Title
::makeTitle( $ns, $dbkey );
357 $cache->addBadLinkObj( $title );
358 $ids[$title->getPrefixedDBkey()] = 0;
361 wfProfileOut( $fname );
366 * Perform the existence test query, return a ResultWrapper with page_id fields
369 $fname = 'LinkBatch::execute';
370 $namespaces = array();
372 if ( !count( $this->data
) ) {
375 wfProfileIn( $fname );
378 // This is very similar to Parser::replaceLinkHolders
379 $dbr =& wfGetDB( DB_SLAVE
);
380 $page = $dbr->tableName( 'page' );
381 $set = $this->constructSet( 'page', $dbr );
382 if ( $set === false ) {
385 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
388 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
389 wfProfileOut( $fname );
394 * Construct a WHERE clause which will match all the given titles.
395 * Give the appropriate table's field name prefix ('page', 'pl', etc).
397 * @param string $prefix
401 function constructSet( $prefix, &$db ) {
405 foreach ( $this->data
as $ns => $dbkeys ) {
406 if ( !count( $dbkeys ) ) {
415 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
418 foreach( $dbkeys as $dbkey => $nothing ) {
424 $sql .= $db->addQuotes( $dbkey );
429 if ( $first && $firstTitle ) {